package edu.ynu.se.xiecheng.achitectureclass.service;


import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;
import edu.ynu.se.xiecheng.achitectureclass.domain.Result;
import edu.ynu.se.xiecheng.achitectureclass.dao.ItemDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.OrderDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.ShopDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.ShopItemDao;
import edu.ynu.se.xiecheng.achitectureclass.domain.vo.*;
import edu.ynu.se.xiecheng.achitectureclass.domain.entity.*;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShopService extends LogicService<ShopDao, Shop, Long> {
    @Resource
    ItemDao itemDao;
    @Resource
    ShopItemDao shopItemDao;
    @Resource
    OrderDao orderDao;

    public ShopService(ShopDao lr) {
        super(lr);
    }

    @Transactional
    public Result addItem(Long shopId, Item item) {
        Shop shop = getDAO().getReferenceById(shopId);
        Item newItem = itemDao.save(item);
        ShopItem shopItem = shop.addItem(newItem);
        getDAO().save(shop);
        return new Result(true, shopItem);
    }

    @Transactional
    public Result saleItem(Long shopId, Long itemId) {
        Item item = new Item();
        item.setId(itemId);
        Shop shop = new Shop();
        shop.setId(shopId);
        ShopItem shopItemByItemAndShop = shopItemDao.findShopItemByItemAndShop(item, shop);
        if (shopItemByItemAndShop == null)
            return new Result("无此商品");
        shop.saleItem(shopItemByItemAndShop);
        ShopItem save = shopItemDao.save(shopItemByItemAndShop);
        if (save == null) {
            return new Result("上架失败");
        }
        return new Result(true, save);
    }

    @Transactional
    public Result stopSale(Long shopId, Long itemId) {
        Item item = new Item();
        item.setId(itemId);
        Shop shop = new Shop();
        shop.setId(shopId);
        ShopItem shopItemByItemAndShop = shopItemDao.findShopItemByItemAndShop(item, shop);
        if (shopItemByItemAndShop == null)
            return new Result("无此商品");
        shop.stopSale(shopItemByItemAndShop);
        ShopItem save = shopItemDao.save(shopItemByItemAndShop);
        if (save == null) {
            return new Result("下架失败");
        }
        return new Result(true, save);
    }

    public Result getAllShop(int page, int size) {
        Pageable pageable = PageRequest.of(page, size, Sort.by("id").descending());
        Page<Shop> all = getDAO().findAll(pageable);
        List<Shop> content = all.getContent();
        List<ShopVo> newList = new ArrayList<>();

        for (int i = 0; i < content.size(); i++) {
            ShopVo shopVo = new ShopVo();
            Business business = content.get(i).getBusiness();
            BusinessVo businessVo = new BusinessVo();
            BeanUtils.copyProperties(content.get(i), shopVo);
            BeanUtils.copyProperties(business, businessVo);
            newList.add(shopVo);
        }
        return new Result(true, newList);
    }

    public Result getShopByBusinessId(int page, int size, Long businessId) {
        Business business = new Business();
        business.setId(businessId);
        Shop shop = new Shop();
        shop.setBusiness(business);
        Pageable pageable = PageRequest.of(page, size, Sort.by("id").descending());
        Example<Shop> shopExample = Example.of(shop);
        List<Shop> content = getDAO().findAll(shopExample, pageable).getContent();
        List<ShopVo> shopVos = new ArrayList<>();

        for (int i = 0; i < content.size(); i++) {
            ShopVo shopVo = new ShopVo();
            Business bus = content.get(i).getBusiness();
            BusinessVo businessVo = new BusinessVo();
            BeanUtils.copyProperties(content.get(i), shopVo);
            BeanUtils.copyProperties(bus, businessVo);
            shopVos.add(shopVo);
        }
        return new Result(true, shopVos);
    }

    //查询未上架商品
    public Result getAllLineItemByShopIdState0(Long shopId) {
        ShopItem shopItem = new ShopItem();

        Shop shop = new Shop();
        shop.setId(shopId);

        shopItem.setShop(shop);
        shopItem.setState(0);

        Example<ShopItem> shopItemExample = Example.of(shopItem);
        List<ShopItem> all = shopItemDao.findAll(shopItemExample);

        List<ItemVo> allItemVo = new ArrayList<>();
        for (int i = 0; i < all.size(); i++) {
            Item item = all.get(i).getItem();
            ItemVo itemVo = new ItemVo();
            BeanUtils.copyProperties(item, itemVo);
            allItemVo.add(itemVo);
        }
        return new Result(true, allItemVo);
    }

    //查询上架商品
    public Result getAllLineItemByShopIdState1(Long shopId) {
        ShopItem shopItem = new ShopItem();
        Shop shop = new Shop();
        shop.setId(shopId);
        shopItem.setShop(shop);
        shopItem.setState(1);
        Example<ShopItem> shopItemExample = Example.of(shopItem);
        List<ShopItem> all = shopItemDao.findAll(shopItemExample);

        List<ItemVo> allItemVo = new ArrayList<>();
        for (int i = 0; i < all.size(); i++) {
            Item item = all.get(i).getItem();
            ItemVo itemVo = new ItemVo();
            BeanUtils.copyProperties(item, itemVo);
            allItemVo.add(itemVo);
        }
        return new Result(true, allItemVo);
    }

    @Transactional
    public Result acceptOrder(Long orderId) {
        Order orderById = orderDao.findOrderById(orderId);
        if (orderById.getPayState() == 0) {
            return new Result(false, "该订单未付款");
        }
        orderById.acceptOrder();
        orderDao.save(orderById);
        return new Result(true, orderById);
    }

    @Transactional
    public Result refuseOrder(Long orderId) {
        Order orderById = orderDao.findOrderById(orderId);

        orderById.refuseOrder();
        orderDao.save(orderById);
        return new Result(true, orderById);
    }

    public Result getAllOrderState2(Long shopId, Integer state, Integer payState) {
        Shop shop = new Shop();
        shop.setId(shopId);
        List<OrderVo> orderVos = new ArrayList<>();
        List<Order> res = orderDao.findOrderByShopAndStateAndPayState(shop, state, payState);
        List<LineItemVo> lineItemVo = new ArrayList<>();
        for (int i = 0; i < res.size(); i++) {
            OrderVo dto = new OrderVo();
            for (int j = 0; j < res.get(i).getLineItems().size(); j++) {
                LineItemVo itemDto = new LineItemVo();
                itemDto.setItemName(res.get(i).getLineItems().get(j).getItem().getName());
                itemDto.setTotalPrice(res.get(i).getLineItems().get(j).getTotalPrice());
                itemDto.setCount(res.get(i).getLineItems().get(j).getCount());
                itemDto.setId(res.get(i).getLineItems().get(j).getId());
                lineItemVo.add(itemDto);
            }

            BeanUtils.copyProperties(res.get(i), dto);
            dto.setLineItemVo(lineItemVo);
            dto.setShopName(res.get(i).getShop().getName());
            orderVos.add(dto);
        }

        if (res == null)
            return new Result("无订单");
        else
            return new Result(true, orderVos);
    }

    public Result getAllOrder(Long shopId) {
        Shop shop = new Shop();
        shop.setId(shopId);
        List<OrderVo> orderVos = new ArrayList<>();
        List<Order> res = orderDao.findOrderByShop(shop);
        List<LineItemVo> lineItemVo = new ArrayList<>();
        for (int i = 0; i < res.size(); i++) {
            OrderVo dto = new OrderVo();
            for (int j = 0; j < res.get(i).getLineItems().size(); j++) {
                LineItemVo itemDto = new LineItemVo();
                itemDto.setItemName(res.get(i).getLineItems().get(j).getItem().getName());
                itemDto.setTotalPrice(res.get(i).getLineItems().get(j).getTotalPrice());
                itemDto.setCount(res.get(i).getLineItems().get(j).getCount());
                itemDto.setId(res.get(i).getLineItems().get(j).getId());
                lineItemVo.add(itemDto);
            }

            BeanUtils.copyProperties(res.get(i), dto);
            dto.setLineItemVo(lineItemVo);
            dto.setShopName(res.get(i).getShop().getName());
            orderVos.add(dto);
        }

        if (res == null)
            return new Result("无订单");
        else
            return new Result(true, orderVos);
    }

}
